package com.ujiuye.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ujiuye.service.SearchService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/search")
public class SearchServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=utf-8");
        String name=req.getParameter("name");
        System.out.println("name="+name);
        SearchService ss=new SearchService();
        List<String> strList=ss.getList(name);
        ObjectMapper om=new ObjectMapper();
        String json=om.writeValueAsString(strList);
        System.out.println("json="+json);
        PrintWriter pw=resp.getWriter();
        pw.print(json);
        pw.close();
    }
}
